<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Image_upload {

   var $CI;

   var $field = '';
   var $upload_path = '';
   var $image_types = 'jpg|jpeg|png|gif';
   var $overwrite = FALSE;
   var $remove_spaces = TRUE;
   var $maintain_ratio = TRUE;

   var $thumb = FALSE;

   // Required if $thumb = TRUE
   var $thumb_path = '';
   var $w_thumb = '';
   var $h_thumb = '';

   var $resize = FALSE;

   // Required if $resize = TRUE
   var $w_max = '';
   var $h_max = '';

   function __construct($params = array())
   {
       $this->CI =& get_instance();

       $this->CI->load->helper('file');
       $this->CI->load->library('image_lib');

       if (count($params) > 0)
       {
           $this->initialize($params);
       }
   }

   /**
    * initialize
    *
    * Set global variables
    *
    * @access public
    * @param void
    * @return void
    */
   function initialize($params = array())
   {
       if (count($params) > 0)
       {
           foreach ($params as $key => $val)
           {
               if (isset($this->$key))
               {
                   $this->$key = $val;
               }
           }
       }
   }

   /**
    * run
    *
    * Full image upload process.
    * 1. Upload image
    * 2. Resize image
    * 3. Create thumbnail
    *
    * @access public
    * @param void
    * @return boolean
    */
	function run()
	{
		$dtls = $this->upload();

		if ( ! is_array($dtls))
		{
		   return $dtls;
		}

		if ($this->resize == TRUE)
		{
		   if ( ! $this->resize($dtls))
		   {
			   return _error('Upload Error: Unable to resize image.');
		   }
		}

		if ($this->thumb == TRUE)
		{
		   if ( ! $this->create_thumb($dtls))
		   {
			   return _error('Upload Error: Unable to create thumbnail.');
		   }
		}

		return $dtls;
	}

   /**
    * clear
    *
    * Reset all global variables
    *
    * @access public
    * @param void
    * @return void
    */
   function clear()
   {
       $this->field = '';
       $this->upload_path = '';
       $this->image_types = 'jpg|jpeg|png|gif';
       $this->overwrite = FALSE;
       $this->remove_spaces = TRUE;
       $this->maintain_ratio = TRUE;
       $this->thumb = FALSE;

       $this->thumb_path = '';
       $this->w_thumb = '';
       $this->h_thumb = '';

       $this->resize = FALSE;

       $this->w_max = '';
       $this->h_max = '';
   }

   /**
    * upload
    *
    * Process image upload.
    * Create thumbnails and/or resize file uploaded.
    *
    * @access public
    * @return boolean
    */
	function upload()
	{
		$upload_config['upload_path'] = $this->upload_path;
		$upload_config['allowed_types'] = $this->image_types;
		$upload_config['remove_spaces'] = $this->remove_spaces;
		$upload_config['overwrite'] = $this->overwrite;

		$this->CI->load->library('upload', $upload_config, 'uploader');
		
		if ( ! $this->CI->uploader->do_upload($this->field))
		{
			return $this->CI->uploader->display_errors('', '');
		}

		$data = $this->CI->uploader->data();
		
		return $data;
	}

   /**
    * resize
    *
    * Resize image according to specified dimensions
    *
    * @access public
    * @param array
    * @return boolean
    */
   function resize($dtls)
   {
       if ($dtls['image_width'] > $this->w_max || $dtls['image_height'] > $this->h_max)
       {
           $resize_config = array(
               'image_library' => 'GD2',
               'source_image' => $dtls['full_path'],
               'maintain_ratio' => TRUE,
               'width' => $this->w_max,
               'height' => $this->h_max,
               'new_image' => $dtls['file_path'],
           );

           $this->CI->image_lib->initialize($resize_config);

           $ret = $this->CI->image_lib->resize();

           $this->CI->image_lib->clear();

           return $ret;
       }

       return TRUE;
   }

   /**
    * create_thumb
    *
    * Create thumbnail for image
    *
    * @access private
    * @param array
    * @return boolean
    */
   function create_thumb($dtls)
   {
       $thumb_config = array(
           'image_library' => 'GD2',
           'source_image' => $dtls['full_path'],
           'maintain_ratio' => $this->maintain_ratio,
           'width' => $this->w_thumb,
           'height' => $this->h_thumb,
           'new_image' => $this->thumb_path,
       );

       $this->CI->image_lib->initialize($thumb_config);

       $ret = $this->CI->image_lib->resize();

       $this->CI->image_lib->clear();

       return $ret;
   }
}
/* End of file Image_upload.php */
/* Location: ./application/libraries/Image_upload.php */
